package com.anhry.app.safety.manage.enterprise.struts.action;

import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.app.safety.manage.enterprise.bean.TbCorpPerson;
import com.anhry.app.safety.manage.enterprise.struts.base.EnterpriseBaseAction;
import com.anhry.app.safety.util.Loggers;
import com.anhry.app.safety.util.Util;

/**
 * @date:2012-8-22
 * @remark:
 * @author:dongbin
 */
@Controller
@Scope("prototype")
@SuppressWarnings("unchecked")
public class CorpPerAction extends EnterpriseBaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String corpId;
	private TbCorpPerson per;
	private String perName;//姓名
	private String perPhone;//电话
	private String perMobile;//手机
	private String perEmail;//邮箱
	private String perType;//类型1：法定代表人2：主要负责人3：安全负责人4：安全管理人员
	private String perSex;//性别 0:女1：男
	private String perDegree;//学历
	private String perTraining;//培训情况
	private String perWorkingYear;//工作年限
	
	/**
	 * 人员信息列表
	 * @return
	 */
	public String list(){
		try {
			if(null!=this.corpId && this.corpId.length()>0){
				this.setObjList((List)this.utilService.getHqlQuery("from TbCorpPerson t where 1=1 and t.corpId="+this.corpId+" order by t.perType"));
				this.setResult("corpPer_list");
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpPerAction:list() 异常："+e.getMessage());
		}
		this.setTip(Util.NOTIP);
		return ERROR;
	}
	
	/**
	 * 跳转到人员添加、修改页面
	 * @return
	 */
	public String edit(){
		try {
			if(null!=this.corpId && this.corpId.length()>0){
				if(null!=this.objId && this.objId.length()>0){
					TbCorpPerson per = (TbCorpPerson)this.utilService.getObjectById(TbCorpPerson.class, Long.parseLong(this.objId));
					this.setPer(per);
				}
				this.setResult("corpPer_edit");
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpPerAction:list() 异常："+e.getMessage());
		}
		this.setTip(Util.NOTIP);
		return ERROR;
	}
	
	/**
	 * 人员保存
	 * @return
	 */
	public String save(){
		try {
			if(null!=this.corpId && this.corpId.length()>0){
				if(null!=this.objId && this.objId.length()>0){
					TbCorpPerson per = (TbCorpPerson)this.utilService.getObjectById(TbCorpPerson.class, Long.parseLong(this.objId));
					per.setPerEmail(this.perEmail);
					per.setPerMobile(this.perMobile);
					per.setPerName(this.perName);
					per.setPerPhone(this.perPhone);
					per.setPerSex(this.perSex);
					per.setPerDegree(this.perDegree);
					per.setPerTraining(this.perTraining);
					per.setPerWorkingYear(this.perWorkingYear);
					this.utilService.update(per);
				}else{
					TbCorpPerson per = new TbCorpPerson();
					per.setPerEmail(this.perEmail);
					per.setPerMobile(this.perMobile);
					per.setPerName(this.perName);
					per.setPerPhone(this.perPhone);
					per.setPerSex(this.perSex);
					per.setPerDegree(this.perDegree);
					per.setPerTraining(this.perTraining);
					per.setPerWorkingYear(this.perWorkingYear);
					per.setPerType("4");
					per.setCorpId(Long.parseLong(this.corpId));
					this.utilService.save(per);
				}
				this.setTip("操作成功！");
				return this.list();
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpPerAction:list() 异常："+e.getMessage());
		}
		this.setTip(Util.NOTIP);
		return ERROR;
	}
	
	/**
	 * 异步删除人员
	 *
	 */
	public void delePer(){
		this.contextPvd.getResponse().setContentType("charset=UTF-8");
		try {
			if(null!=this.objId && this.objId.length()>0){
				TbCorpPerson per = (TbCorpPerson)this.utilService.getObjectById(TbCorpPerson.class, Long.parseLong(this.objId));
				if(null!=per){
					this.utilService.delete(per);
				}
				this.contextPvd.getResponse().getWriter().write("1");
			}else{
				this.contextPvd.getResponse().getWriter().write("2");
			}
			this.contextPvd.getResponse().getWriter().flush();
			this.contextPvd.getResponse().getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("manager:CorpPerAction:delePer()方法出错："+e.getMessage());
		}
	}

	public String getCorpId() {
		return corpId;
	}

	public void setCorpId(String corpId) {
		this.corpId = corpId;
	}

	public TbCorpPerson getPer() {
		return per;
	}

	public void setPer(TbCorpPerson per) {
		this.per = per;
	}

	public String getPerEmail() {
		return perEmail;
	}

	public void setPerEmail(String perEmail) {
		this.perEmail = perEmail;
	}

	public String getPerMobile() {
		return perMobile;
	}

	public void setPerMobile(String perMobile) {
		this.perMobile = perMobile;
	}

	public String getPerName() {
		return perName;
	}

	public void setPerName(String perName) {
		this.perName = perName;
	}

	public String getPerPhone() {
		return perPhone;
	}

	public void setPerPhone(String perPhone) {
		this.perPhone = perPhone;
	}

	public String getPerSex() {
		return perSex;
	}

	public void setPerSex(String perSex) {
		this.perSex = perSex;
	}

	public String getPerType() {
		return perType;
	}

	public void setPerType(String perType) {
		this.perType = perType;
	}

	public String getPerDegree() {
		return perDegree;
	}

	public void setPerDegree(String perDegree) {
		this.perDegree = perDegree;
	}

	public String getPerTraining() {
		return perTraining;
	}

	public void setPerTraining(String perTraining) {
		this.perTraining = perTraining;
	}

	public String getPerWorkingYear() {
		return perWorkingYear;
	}

	public void setPerWorkingYear(String perWorkingYear) {
		this.perWorkingYear = perWorkingYear;
	}

}
